package com.hopes.bizz.basic.gis.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicModDictEnum;
import com.hopes.bizz.basic.common.constant.enums.ParkBasicSysDictEnum;
import com.hopes.bizz.basic.gis.vo.ThirdInfoVo;
import com.hopes.bizz.basic.gis.vo.ThirdRecordVo;
import com.hopes.bizz.basic.third.entity.*;
import com.hopes.bizz.basic.third.service.*;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: ljh
 * @create: 2024/1/18
 */
@Service
@RequiredArgsConstructor
public class BaseGisService {

    // 资质
    private final BaseThirdEmployeeCertificateService baseThirdEmployeeCertificateService;
    // 违规纪录
    private final BaseThirdViolateRecordService baseThirdViolateRecordService;
    // 培训纪录
    private final BaseThirdTrainRecordService baseThirdTrainRecordService;
    // 服务纪录
    private final BaseThirdServerRecordService baseThirdServerRecordService;
    // 评价评估
    private final BaseThirdEvaluateService baseThirdEvaluateService;

    public ThirdInfoVo getThirdInfo() {
        ThirdInfoVo thirdInfoVo = new ThirdInfoVo();
        JSONArray certificationStatus = new JSONArray();
        JSONArray evaluateResult = new JSONArray();
        // 统计第三方资质
        List<BaseThirdEmployeeCertificateEntity> certificateEntities = baseThirdEmployeeCertificateService.list();
        thirdInfoVo.setCertificationTotal(certificateEntities.size());
        thirdInfoVo.setCertificationStatus(certificationStatus);
        // 证书状态统计
        Map<String, Long> certificateStatusMap = certificateEntities.stream().collect(Collectors.groupingBy(BaseThirdEmployeeCertificateEntity::getCertificateStatus, Collectors.counting()));
        certificateStatusMap.forEach((k, v) -> {
            JSONObject data = new JSONObject();
            data.put("name", DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.certificate_status, k));
            data.put("value", v);
            certificationStatus.add(data);
        });
        // 第三方评估结果统计
        List<BaseThirdEvaluateEntity> evaluateEntities = baseThirdEvaluateService.list();
        thirdInfoVo.setEvaluateResult(evaluateResult);
        Map<String, Long> evaluateResultMap = evaluateEntities.stream().collect(Collectors.groupingBy(BaseThirdEvaluateEntity::getEvaluateResult, Collectors.counting()));
        evaluateResultMap.forEach((k,v)->{
            JSONObject data = new JSONObject();
            data.put("name",DictResolver.getItemLabelByValue(ParkBasicSysDictEnum.assess_result,k));
            data.put("value",v);
            evaluateResult.add(data);
        });
        return thirdInfoVo;
    }

    // 第三方相关纪录统计
    public ThirdRecordVo getThirdRecord() {
        ThirdRecordVo thirdRecordVo = new ThirdRecordVo();
        List<BaseThirdTrainRecordEntity> trainRecordEntities = baseThirdTrainRecordService.list();
        // 培训纪录统计
        Map<Long, Long> trainMap = trainRecordEntities.stream().collect(Collectors.groupingBy(BaseThirdTrainRecordEntity::getEmployerId, Collectors.counting()));
        List<BaseThirdViolateRecordEntity> violateRecordEntities = baseThirdViolateRecordService.list();
        // 违规纪录统计
        Map<Long, Long> violateMap = violateRecordEntities.stream().collect(Collectors.groupingBy(BaseThirdViolateRecordEntity::getEmployerId, Collectors.counting()));
        // 服务纪录统计
        List<BaseThirdServerRecordEntity> serverRecordEntities = baseThirdServerRecordService.list();
        Map<Long, Long> serverMap = serverRecordEntities.stream().collect(Collectors.groupingBy(BaseThirdServerRecordEntity::getEmployerId, Collectors.counting()));
        ArrayList<Long> train = new ArrayList<>();
        ArrayList<Long> violate = new ArrayList<>();
        ArrayList<Long> server = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();
        for (SysDictItem thirdUnit : DictResolver.getItemsByType(ParkBasicModDictEnum.third_unit)) {
            name.add(thirdUnit.getName());
            train.add(trainMap.getOrDefault(thirdUnit.getId(),0L));
            violate.add(violateMap.getOrDefault(thirdUnit.getId(),0L));
            server.add(serverMap.getOrDefault(thirdUnit.getId(),0L));
        }
        thirdRecordVo.setServeCount(serverRecordEntities.size());
        thirdRecordVo.setViolateCount(violateRecordEntities.size());
        thirdRecordVo.setTrainCount(trainRecordEntities.size());
        JSONObject recordBarData = new JSONObject();
        recordBarData.put("train",train);
        recordBarData.put("violate",violate);
        recordBarData.put("server",server);
        recordBarData.put("name",name);
        thirdRecordVo.setRecordBarData(recordBarData);
        return thirdRecordVo;
    }
}
